﻿using System;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;


namespace AnyOffice
{

    public partial class validate_code :Page
    {
        private string ValidateCode(int codeCount)
        {
            string[] strArray = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z".Split(new char[] { ',' });
            string str2 = "";
            int num = -1;
            Random random = new Random();
            for (int i = 0; i < codeCount; i++)
            {
                if (num != -1)
                {
                    random = new Random((i * num) * ((int)DateTime.Now.Ticks));
                }
                int index = random.Next(35);
                if (num == index)
                {
                    return this.ValidateCode(codeCount);
                }
                num = index;
                str2 = str2 + strArray[index];
            }
            return str2;
        }
        private void ValidateImage(string ValidateCode)
        {
            Bitmap image = new Bitmap(Server.MapPath("./images/ValidateCode.jpg"));
            Graphics graphics = Graphics.FromImage(image);
            graphics.SmoothingMode = SmoothingMode.AntiAlias;
            graphics.DrawString(ValidateCode, new Font("Arial", 12f, FontStyle.Bold), SystemBrushes.WindowText, (PointF)new Point(5, 2));
            Response.ContentType = "image/jpeg";
            image.Save(Response.OutputStream, ImageFormat.Jpeg);
            graphics.Dispose();
            image.Dispose();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.AppendHeader("Expires", DateTime.Now.AddDays(-1.0).ToString("r"));
            Response.AppendHeader("Pragma", "no-cache");
            Response.AppendHeader("Cache-Control", "no-cache");
            string validateCode = this.ValidateCode(4);
            this.Session["VerifyCode"] = validateCode;
            this.ValidateImage(validateCode);
        }

 

    }
}
